package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author 苏成博
 * @date 2023-02-09 16:02
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理相关接口")
public class SetmealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "cacheSetmeal",key = "#setmealDTO.categoryId")
    public Result saveSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setMealService.saveSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult= setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("删除操作")
    @CacheEvict(cacheNames = "cacheSetmeal",allEntries = true)
    public Result deleteSetmeal(String ids){
        String[] split = ids.split(",");
        //循环删除缓存里面的东西
        for (int i = 0; i < split.length; i++) {
            redisTemplate.delete("dish_"+split[i]);
        }
        setMealService.deleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 更新套餐状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("更新套餐状态")
    @CacheEvict(cacheNames = "cacheSetmeal",key = "#categoryId")
    public Result updateStatus(@PathVariable Integer status,Long id,Integer categoryId){
        //删除缓存
        redisTemplate.delete("dish_"+id);
        Setmeal setmeal = setMealService.findById(id);
        categoryId=setmeal.getCategoryId().intValue();
        setMealService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐信息")
    public Result<SetmealVO> findById(@PathVariable Integer id){
        SetmealVO setmealVO=setMealService.findById(id);
        return Result.success(setmealVO);
    }
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "cacheSetmeal",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        redisTemplate.delete("dish_"+setmealDTO.getId());
        setMealService.updateSetmeal(setmealDTO);
        return Result.success();
    }
}
